package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.ClientCompanyMapper;
import com.erp.programmer.dao.SaleOrderMapper;
import com.erp.programmer.domain.*;
import com.erp.programmer.domain.SaleOrder;
import com.erp.programmer.dto.*;
import com.erp.programmer.dto.SaleOrderDto;
import com.erp.programmer.enums.PaymentStateEnum;
import com.erp.programmer.service.ISaleOrderDetailService;
import com.erp.programmer.service.ISaleOrderService;
import com.erp.programmer.service.ProductService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Takagi3
 * @since 2021-05-24
 */
@Service
public class SaleOrderServiceImpl extends ServiceImpl<SaleOrderMapper, SaleOrder> implements ISaleOrderService {

    @Autowired
    SaleOrderMapper saleOrderMapper;
    @Autowired
    ProductService productService;
    @Autowired
    ISaleOrderService iSaleOrderService;
    @Autowired
    ISaleOrderDetailService iSaleOrderDetailService;

    @Autowired
    private ClientCompanyMapper clientCompanyMapper;

    @Override
    public  ResponseDto<List<SaleOrderDto>> getData( ) {
        QueryWrapper<SaleOrder> queryWrapper = new QueryWrapper<>();
        List<SaleOrder> clientCompaniesListDb = saleOrderMapper.selectList(queryWrapper);
        List<SaleOrderDto> saleOrderDtoList = CopyUtil.copyList(clientCompaniesListDb, SaleOrderDto.class);
        return ResponseDto.success(saleOrderDtoList);
    }


    /**分页
     *
     *  新增销售订单
     * **/
    @Override
    public ResponseDto<PageDto<SaleOrderDto>> saveSaleOrder( SaleOrderDto saleOrderDto)
    {

        // 生成销售单ID
        String saleOrderId = String.valueOf(IdWorker.getId());
        //定义销售单总价
        BigDecimal totalPrice = new BigDecimal(0);

        for(SaleOrderDetailDto saleOrderDetailDto:saleOrderDto.getSaleOrderDetailDtos()) {
            //获取详单的商品数量
            BigDecimal bigDecimal = new BigDecimal(saleOrderDetailDto.getNumber());
            //获取详单的总价
            BigDecimal price = productService.getById(saleOrderDetailDto.getProductId()).getPrice().multiply(bigDecimal);
            //设置详单外键，订单ID
            saleOrderDetailDto.setSaleOrderNumber(saleOrderId);
            //详单总价累加，得到订单总价
            totalPrice=totalPrice.add(price);
            //设置详单总价
            saleOrderDetailDto.setTotalPrice(price);
            //Dto转换，数据库新增详单
            if(!iSaleOrderDetailService.save(CopyUtil.copy(saleOrderDetailDto, SaleOrderDetail.class))){
                return ResponseDto.errorByMsg(CodeMsg.SUBMIT_ORDER_ERROR);
            }
        }
        //设置订单数据
        saleOrderDto.setId(saleOrderId);
        saleOrderDto.setTotalPrice(totalPrice);

        saleOrderDto.setPaymentState(PaymentStateEnum.UNPAID.getCode());
        //Dto转换
        SaleOrder saleOrder = CopyUtil.copy(saleOrderDto, SaleOrder.class);
        saleOrder.setOrderData(new Date());
        if(saleOrderMapper.insert(saleOrder)<1) {
            return ResponseDto.errorByMsg(CodeMsg.SUBMIT_ORDER_ERROR);
        }

        // List<SaleOrderDto> saleOrderDtoList = CopyUtil.copyList(list(), SaleOrderDto.class);

        ResponseDto<PageDto<SaleOrderDto>> saleOrderOrder1 = getSaleOrders(new PageDto<SaleOrderDto>());
        saleOrderOrder1.setMsg("添加成功");

        return saleOrderOrder1;
    }


    /**
     * 根据条件获取订单信息，无条件返回全部
     * @param pageDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<SaleOrderDto>> getSaleOrders(PageDto<SaleOrderDto> pageDto) {
        QueryWrapper<SaleOrder> queryWrapper = new QueryWrapper<>();
        if ("id".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())) {
            queryWrapper.eq("id",pageDto.getSearchContent());
        }
        if (pageDto.getPage() ==null) {
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<SaleOrder> saleOrderList = saleOrderMapper.selectList(queryWrapper);
        List<SaleOrderDto> saleOrderOrderDtoList = CopyUtil.copyList(saleOrderList, SaleOrderDto.class);
        saleOrderOrderDtoList.forEach(e->{
            ClientCompany clientCompanyDb = clientCompanyMapper.selectById(e.getClientCompanyId());
            ClientCompanyDto clientCompanyDto = CopyUtil.copy(clientCompanyDb, ClientCompanyDto.class);
            e.setClientCompanyDto(clientCompanyDto);
        });
        PageInfo<SaleOrder> pageInfo = new PageInfo<>(saleOrderList);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(saleOrderOrderDtoList);
        return ResponseDto.success(pageDto);
    }


    /**
     * 修改订单，设置完成时间。
     *
     */
    @Override
    public ResponseDto<PageDto<SaleOrderDto>> updateCompleteDate(SaleOrderDto saleOrderDto) {
        // 1.转换
        SaleOrder saleOrder = CopyUtil.copy(saleOrderDto, SaleOrder.class);
        saleOrder.setCompleteData(new Date());
        // 2.业务逻辑
        saleOrderMapper.updateById(saleOrder);


        PageDto<SaleOrderDto> pageDtoList = getSaleOrders(new PageDto<SaleOrderDto>()).getData();
        ResponseDto<PageDto<SaleOrderDto>> responseDto = getSaleOrders(pageDtoList);
        responseDto.setMsg("完成时间更新成功！");

        return responseDto;
    }

    /**
     * 修改付款状态 未付款1  已付款2
     * @return
     */
    @Override
    public ResponseDto<PageDto<SaleOrderDto>> updatePaymentState(EditStateDto editStateDto) {

        SaleOrder saleOrder = saleOrderMapper.selectById(editStateDto.getId());
        //Id验证
        if (saleOrder==null) {
            return ResponseDto.errorByMsg(CodeMsg.ORDER_NOT_EXIST);
        }
        SaleOrderDto saleOrderDto = CopyUtil.copy(saleOrder, SaleOrderDto.class);
        saleOrderDto.setPaymentState(Integer.valueOf(editStateDto.getState()));
        saleOrder = CopyUtil.copy(saleOrderDto, SaleOrder.class);
        //数据库更新支付标志
        if (saleOrderMapper.updateById(saleOrder)<1) {
            return ResponseDto.errorByMsg(CodeMsg.CREATE_PAY_ERROR);
        }
        ResponseDto<PageDto<SaleOrderDto>> saleOrderDto1 = getSaleOrders(new PageDto<SaleOrderDto>());
        saleOrderDto1.setMsg("支付状态变更成功!");
        return saleOrderDto1;
    }
}




